//
//  HomeOrderReviewController.m
//  RentTable
//
//  Created by mac on 2024/11/30.
//

#import "HomeOrderReviewController.h"
#import "HotTableCell.h"
#import "HomeTableDetailController.h"
#import "HomeOrderInfoController.h"

@interface HomeOrderReviewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *orderInfoButton;

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray<RentModel *> *products;

@end

@implementation HomeOrderReviewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self loadProductData];
    [self setUI];
}

- (void)loadProductData {
    RentManager *rentManager = [RentManager sharedManager];
    NSMutableArray<RentModel *> *rentModels = [rentManager getAllRentData];
    self.products = rentModels;
    
}

- (void)setUI {
    
    UIImageView *reviewBackView = [UIImageView new];
    reviewBackView.image = [UIImage imageNamed:@"蒙版分组"];
    [reviewBackView setUserInteractionEnabled:true];
    [self.view addSubview:reviewBackView];
    [reviewBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self loadBackArrow];
    [self loadTitle:@"订单审核"];
    
    UIImageView *reviewIconView = [UIImageView new];
    reviewIconView.image = [UIImage imageNamed:@"空数据"];
    [reviewBackView addSubview:reviewIconView];
    [reviewIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(119 * scaleH);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(175 * scaleH, 175 * scaleH));
    }];
    
    UILabel *sumLabel = [[UILabel alloc] init];
    sumLabel.text = @"订单审核中";
    sumLabel.font = [UIFont pingFangSCRegularWithSize:17];
    sumLabel.textColor = myRGBHex(0x333333);
    [reviewBackView addSubview:sumLabel];
    [sumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(reviewIconView.mas_bottom).offset(40 * scaleH);
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(21 * scaleH);
    }];
    
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancelButton setTitle:@"取消订单" forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:myRGBHex(0xFFFFFF) forState:UIControlStateNormal];
    self.cancelButton.titleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    [self.cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [reviewBackView addSubview:self.cancelButton];
    [self.cancelButton.titleLabel sizeToFit];
    self.cancelButton.frame = CGRectMake(68 * scaleW, 403 * scaleH, 90 * scaleW, 34);
    self.cancelButton.layer.cornerRadius = 17;
    self.cancelButton.layer.masksToBounds = true;
    [CommondHandle gradientTheView:self.cancelButton bounds:self.cancelButton.bounds colors:@[myRGBHex(0xE375FF),myRGBHex(0xFF0AA1)] startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5) hasCornerRadius:17 shouldTakeBack:false];
    
    
    self.orderInfoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.orderInfoButton setTitle:@"订单详情" forState:UIControlStateNormal];
    [self.orderInfoButton setTitleColor:myRGBHex(0xFFFFFF) forState:UIControlStateNormal];
    self.orderInfoButton.titleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    [self.orderInfoButton addTarget:self action:@selector(orderInfoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [reviewBackView addSubview:self.orderInfoButton];
    [self.orderInfoButton.titleLabel sizeToFit];
    self.orderInfoButton.frame = CGRectMake((spDeviceWidth - 157 * scaleW), 403 * scaleH, 90 * scaleW, 34);
    self.orderInfoButton.layer.cornerRadius = 17;
    self.orderInfoButton.layer.masksToBounds = true;
    self.orderInfoButton.backgroundColor = myRGBHex(0x121212);
    
    UIView *recomdView = [UIView new];
    recomdView.backgroundColor = myRGBHex(0xFFFFFF);
    recomdView.frame = CGRectMake(16, spDeviceHeight - 300 * scaleH, spDeviceWidth- 32, 300 * scaleH);
    [reviewBackView addSubview:recomdView];
    
    [recomdView setCornerRadius:20 forCorners:UIRectCornerTopLeft | UIRectCornerTopRight borderColor:myRGBHex(0xFFFFFF) borderWidth:1];
    
    UIImageView *recomdImageView = [UIImageView new];
    recomdImageView.image = [UIImage imageNamed:@"为你推荐"];
    [recomdView addSubview:recomdImageView];
    [recomdImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(150, 34 * scaleH));
    }];
    
    [self setupCollectionView:recomdView];
}

- (void)setupCollectionView:(UIView *)hotBackView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((spDeviceWidth - 76) / 2.0, 175 * scaleH);
    layout.sectionInset = UIEdgeInsetsMake(10, 16, 10, 16);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[HotTableCell class] forCellWithReuseIdentifier:@"HotTableCell"];
    
    [hotBackView addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(57 * scaleH);
        make.leading.trailing.bottom.mas_equalTo(0);
    }];
}

#pragma mark - UICollectionViewDelegate & DataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.products.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HotTableCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotTableCell" forIndexPath:indexPath];
    
    RentModel *product = self.products[indexPath.item];
    
    [cell setPrice:[NSString stringWithFormat:@"%.1f", product.rentPrice] describe:product.rentTitle image:product.rentImage];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeTableDetailController *vc = [HomeTableDetailController new];
    RentModel *product = self.products[indexPath.item];
    vc.rentModel = product;
    vc.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:vc animated:true];
}

- (void)cancelButtonPressed:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:false];
    [RentModelManager removeRentModelByRentBuyId:self.rentModel.rentBuyId];
}

- (void)orderInfoButtonPressed:(UIButton *)sender {
    HomeOrderInfoController *vc = [HomeOrderInfoController new];
    vc.rentModel = self.rentModel;
    vc.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:vc animated:true];
}


@end
